﻿package 
{
	//WEEK 05
	import flash.display.MovieClip;
	import flash.display.FrameLabel;
	import flash.events.*;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	import flash.geom.Point;
	//bullet class
	import bullet;
	
	//ADDED wk 6
	import flash.utils.*;
	import Level;
	import Character;

	public class GameManager extends MovieClip{
		static public const MENU:String = "menu";
		static public const GAME:String = "game";
		static public const END:String = "end";		
	
		// constant for the speed of the bullet
		public const BULLETPOWER:int = 9;

		//holds a reference to the bullet when it is there
		public var bulletRef:bullet = null;	
		
		//ADDED WK6 level handling variables
		private var levels:Array = new Array("Level01","Level02")
		public var levelMC:Level = null;
		public var character:Character = null;
		
		//Score and Lives stored here:
		public var score:int = 0;
		public var lives:int = 3;
		
		
		public function GameManager()
		{
			//setup frames of main movie to send an event when they are reached
			enumerateFrameLabels();
			addEventListener(MENU, setupMenu,false,0,true);
			addEventListener(GAME, setupGame,false,0,true);
			addEventListener(END, setupEnd,false,0,true);
		}
		
		private function dispatchFrameEvent():void{
			stop();
			dispatchEvent(new Event(currentLabel));
		}
		private function enumerateFrameLabels():void
		{
			for each (var label:FrameLabel in currentLabels)
				addFrameScript(label.frame-1, dispatchFrameEvent);
		}
		
		private function setupMenu(e:Event)
		{
			test_button.button_text.text = "Start";
			test_button.addEventListener(MouseEvent.CLICK, aClick,false,0,true);
		}
		
		private function setupGame(e:Event)
		{
			//end_button.button_text.text = "Win";
			//end_button.addEventListener(MouseEvent.CLICK, bClick,false,0,true);

			//add fade in ?
			
			//ADDED wk 6
			//set score to 0 and lives to 3
			score = 0;
			lives = 3;
			
			//add a character to the stage (off stage)
			character = new Character(); 
			addChildAt(character,1);
			// move the character off stage until the level is ready
			character.x = -32;
			character.y = -32;
			// load the level
			loadLevel("Level01");
			
			// add the main game loop listener
			addEventListener(Event.ENTER_FRAME,update,false,0,true);
		}
		private function setupEnd(e:Event)
		{
			
			start_button.button_text.text = "Menu";
			start_button.addEventListener(MouseEvent.CLICK, cClick,false,0,true);

			//check lives and score
			if(lives>0){
				//win
				if(score==0){
					endText.text = "Sneaking Success\r\r";
				}else {
					endText.text = "Killing Success\r\r";
				}
				endText.appendText("You have escaped the castle of doom with the magic fish and the 
								   powerful chicken.");
			}else{
				//loose
				endText.text = "You died trying";
			}
			
			
		}
		
		private function cleanUpGame()
		{
			//remove Enter frame listener
			removeEventListener(Event.ENTER_FRAME,update);
			removeChild(character);
			character = null;
			removeChild(levelMC);
			levelMC = null;
		}
		
		//ADDED WK6
		private function loadLevel(levelName:String)
		{
			// if there is a level already loaded then clean up the old level
			if(levelMC!=null){
				removeChild(levelMC);
				levelMC = null;
			}
			// get the definition of the class from the string that was passed in 
     		var ClassReference:Class = getDefinitionByName(levelName) as Class
			//create a new version of that 
			levelMC = new ClassReference(); 
			
			addEventListener("LevelLoaded",startLevel,false,0,true);
			addChildAt(levelMC,1);
			
			
		}
		
		//ADDED WK6
		public function startLevel()
		{
			//Move the character off stage whilst the next level loads
			character.x = -32;
			character.y = -32;
			character._NextX = -32;
			character._NextY = -32;
		}
		
		//ADDED WK6
		public function exitLevel()
		{
			//move the character off screen first
			startLevel();
			
			// look up current level
			var currpos:int =  levels.indexOf(levelMC.levelName);
			if(currpos!=-1 && currpos+1<levels.length){
				//get the next
				loadLevel(levels[currpos+1]);
				
			}else{
				exitGame();
			}
				
		}
		
		//ADDED WK6
		//made public
		public function exitGame()
		{
			cleanUpGame();
			gotoAndPlay(END);
		}
		

		//START Button Management Code		
		private function aClick(e:Event)
		{
			gotoAndPlay(GAME);
		}
		private function bClick(e:Event)
		{
			gotoAndPlay(END);
		}
		private function cClick(e:Event)
		{
			gotoAndPlay(MENU);
		}
		//END Button Management
		

	
		//MAIN GAME LOOP
		private function update(e:Event)
		{
			if(character!=null){
				character.nextPosition();
				character.checkCharacterCollision();
				character.makeMove();
			}
			
			if(bulletRef!=null){
				bulletRef.moveBullet();
			}
			
			// if you have no lives you die
			if(lives<=0){
				exitGame();
			}
			// update score and lives displays
			scoreMC.textblock.text = "Score: " +  score.toString();
			livesMC.textblock.text = "Lives: " +  lives.toString();
			
			
		}	
	}
}